package com.lrs.java.dataStructure.linkList;

class LinkList {

	private Link first;

	public LinkList() {
		first = null;
	}
	
	public void insertFirst(int i, double d) {
		Link newLink = new Link(i, d);
		newLink.next = first;
		first = newLink;
	}

	public Link removeFirst() {
		Link temp = first;
		first = first.next;
		return temp;
	}

	public Link find(int key) {
		Link current = first;
		
		while(current != null) {
			if(current.getiData() == key) {
				return current;
			}
			current = current.next;
		}
		
		return null;
	}
	
	public Link remove(int key) {
		Link current = first;
		Link previous = first;
		
		if(current == null) {
			return null;
		}
		
		while(current.getiData() != key) {
			if(current.next == null) {
				return null;
			}
			
			previous = current;
			current = current.next;
		}

		if(current == first) {
			first = first.next;
		} else {
			previous.next = current.next;
		}
		
		return current;
	}
	
	
	public boolean isEmpty() {
		return first == null;
	}
	
	
	public void display() {
		Link current = first;
		while(current != null) {
			System.out.print("(" + current.getiData() + " " + current.getdData() + ")-->");
			current = current.next;
		}
		System.out.println();
	}
	
}

class LinkListApp {
	public static void main(String[] args) {
		
		LinkList list = new LinkList();
		list.insertFirst(1, 1.0);
		list.insertFirst(2, 2.0);
		list.insertFirst(3, 3.0);
		list.insertFirst(4, 4.0);
		list.display();
		
		Link l3 = list.find(3);
		System.out.println("Link 3 (" + l3.getiData() + " " + l3.getdData() + ")");
		
		list.remove(3);
		list.removeFirst();
		list.removeFirst();
		
		list.display();
		
	}
}
